package com.syni.mdd.yhd.common.all.service.impl.market;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.market.BmsMarketReceDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessCoupon;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import com.syni.mdd.yhd.common.all.entity.market.BmsMarketCoupon;
import com.syni.mdd.yhd.common.all.entity.market.BmsMarketRece;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsBusinessCouponService;
import com.syni.mdd.yhd.common.all.service.BmsUserReceCouponService;
import com.syni.mdd.yhd.common.all.service.BmsVerRecordService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.service.market.BmsMarketCouponService;
import com.syni.mdd.yhd.common.all.service.market.BmsMarketReceService;
import com.syni.mdd.yhd.common.all.utils.OrderUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className
 * @description TOO
 * @Author hwq
 * @DATE 2019/9/23 10:34
 * @VERSION 1.0
 **/
@Service
public class BmsMarketReceServiceImpl extends BaseService<BmsMarketRece, Integer> implements BmsMarketReceService {

    @Autowired
    BmsMarketReceDao bmsMarketReceDao;

    @Autowired
    BmsMarketCouponService bmsMarketCouponService;

    @Autowired
    BmsBusinessCouponService businessCouponService;

    @Autowired
    BmsUserReceCouponService bmsUserReceCouponService;

    @Autowired
    BmsVerRecordService bmsVerRecordService;


    @Override
    public BmsMarketReceDao getBaseDao() {
        return bmsMarketReceDao;
    }

    @Override
    public Map<String, Object> userReceCoupon(ParamsMap paramsMap) {
        String openId = paramsMap.getString("openId");
        String prizeId = paramsMap.getString("prizeId");
        BmsMarketRece bmsMarketRece = new BmsMarketRece();
        bmsMarketRece.setPrizeId(prizeId);
        bmsMarketRece.setOpenId(openId);
        bmsMarketRece.setNewTime(new Date());
        bmsMarketReceDao.save(bmsMarketRece);
        return ResultMapHelper.success(bmsMarketRece,ResultMsg.HAND_SUCCESS);
    }

    @Override
    public Map<Integer,BmsBusinessCoupon> handleReceCoupon(String openId) {
        Map<Integer,BmsBusinessCoupon> bmsBusinessCouponMap = new HashMap<>();
        BmsUser bmsUser = TokenManager.getUser(openId);
        if(bmsUser != null) {
            List<BmsMarketRece> bmsMarketReces = bmsMarketReceDao.findByOpenIdAndIsHandle(openId, Constants.COMMON_FLAG_FALSE_INT);
            if (bmsMarketReces != null && bmsMarketReces.size() > 0) {
                Map<String,BmsMarketRece> bmsMarketReceMap = new HashMap<>();
                for(BmsMarketRece bmsMarketRece : bmsMarketReces){
                    bmsMarketReceMap.put(bmsMarketRece.getPrizeId(),bmsMarketRece);
                }
                Set<String> prizeIds = bmsMarketReces.stream().map(BmsMarketRece::getPrizeId).collect(Collectors.toSet());
                if (prizeIds.size() > 0) {
                    List<BmsMarketCoupon> bmsMarketCoupons = bmsMarketCouponService.getBaseDao().findByPrizeIdIn(prizeIds);
                    if (bmsMarketCoupons != null && bmsMarketCoupons.size() > 0) {
                        Set<Integer> couponIds = bmsMarketCoupons.stream().map(BmsMarketCoupon::getCouponId).collect(Collectors.toSet());
                        List<BmsUserReceCoupon> bmsUserReceCoupons = new ArrayList<>();
                        List<BmsMarketRece> bmsMReces = new ArrayList<>();
                        bmsBusinessCouponMap = businessCouponService.getBusinessCoupon(couponIds);
                        for (BmsMarketCoupon bmsMarketCoupon : bmsMarketCoupons) {
                            if(bmsBusinessCouponMap.containsKey(bmsMarketCoupon.getCouponId())) {
                                BmsBusinessCoupon businessCoupon = bmsBusinessCouponMap.get(bmsMarketCoupon.getCouponId());
                                BmsUserReceCoupon bmsUserReceCoupon = new BmsUserReceCoupon();
                                bmsUserReceCoupon.setType(Constants.BUSINESS_PRODUCE);
                                bmsUserReceCoupon.setUserId(bmsUser.getId());
                                bmsUserReceCoupon.setBusinessId(businessCoupon.getBusinessId());
                                bmsUserReceCoupon.setBusinessCouponId(businessCoupon.getId());
                                bmsUserReceCoupon.setStatus(Constants.COMMON_FLAG_FALSE_INT);
                                bmsUserReceCoupon.setConsumerCode(OrderUtils.getGroupBuyPayNo(false));
                                bmsUserReceCoupon.setNewTime(new Date());
                                bmsUserReceCoupon.setExpireTime(businessCoupon.getEndTime());
                                bmsUserReceCoupons.add(bmsUserReceCoupon);
                                if(bmsMarketReceMap.containsKey(bmsMarketCoupon.getPrizeId())){
                                    BmsMarketRece bmsMarketRece = bmsMarketReceMap.get(bmsMarketCoupon.getPrizeId());
                                    bmsMarketRece.setIsHandle(Constants.COMMON_FLAG_TRUE_INT);
                                    bmsMReces.add(bmsMarketRece);
                                }
                            }
                        }
                        if(bmsUserReceCoupons.size() > 0){
                            bmsUserReceCouponService.getBaseDao().saveAll(bmsUserReceCoupons);
                            bmsUserReceCouponService.getBaseDao().flush();
                            bmsVerRecordService.addRecordByReceQuan(bmsUserReceCoupons);

                        }
                        if(bmsMReces.size() > 0){
                            bmsMarketReceDao.saveAll(bmsMReces);
                        }
                    }
                }
            }
        }
        return bmsBusinessCouponMap;
    }
}
